require 5.006;
package Chimera::CGI;
use strict;
use HTTP::Request;

# additional functionality (parse post as get request)
$Chimera::CGI::_hybrid = 0;

sub new {
    my ($class, $req, $data) = @_;
    my $self = {};

    $self->{_req}    = HTTP::Request->parse($req);
    $self->{_data}   = $data;
    $self->{_header} = {};

    $self = bless($self, $class);

    $self->_env();
    $self->_read_form();

    return $self;
}

sub _env {
    my $self = shift;

#    $ENV{'REMOTE_ADDR'}     = $clients->{$handle}{ip};
#    $ENV{'REMOTE_HOST'}     = $clients->{$handle}{host};
#    $ENV{'SERVER_NAME'}     = $host;
#    $ENV{'SERVER_PORT'}     = $port;
    $ENV{'CONTENT_TYPE'}    = $self->{_req}->header('Content-type') ?
                              $self->{_req}->header('Content-type') : undef;
    $ENV{'CONTENT_LENGTH'}  = $self->{_req}->header('Content-length') ?
                              $self->{_req}->header('Content-length') : undef;
    $ENV{'SERVER_PROTOCOL'} = $self->{_req}->protocol;
    $ENV{'REQUEST_METHOD'}  = $self->{_req}->method;
    $ENV{'REQUEST_URI'}     = $self->{_req}->uri;
    ($ENV{'PATH_INFO'},
     $ENV{'QUERY_STRING'})  = ($ENV{'REQUEST_URI'} =~ m/([^?]*)(?:\?(.*))?/s);

    return $self;
}

sub _read_form {
    my $self = shift;

    if (defined $ENV{'REQUEST_METHOD'}) {
        if ($ENV{'REQUEST_METHOD'} eq 'POST') {
            $self->_read_post;
            if ($BlackMamba::CGI::_hybrid) {
                $self->_read_get;
            }
        } elsif (($ENV{'REQUEST_METHOD'} eq 'GET') ||
                 ($ENV{'REQUEST_METHOD'} eq 'HEAD')) {
            $self->_read_get;
        }
    }
}

sub _read_post {
    my $self = shift;

    if ($ENV{'CONTENT_TYPE'} =~ m/^multipart\/form-data; boundary=(.*)$/i) {
        # multipart (for file)
        # now, i am not going to implement this functionality,
        # because the speed is an important factor
    } else {
        $self->_url_encode_data($self->{_data}, '[;&]');
    }
}

sub param {
    my $self = shift;

    if ($#_ == -1) {
        return keys %{$self->{_params}};
    } else {
        my $name = shift;
        if (defined $self->{_params}{$name}) {
            return $self->{_params}{$name};
        }
    }
}

sub _read_get {
    my $self = shift;
    $self->_url_encode_data($ENV{'QUERY_STRING'}, '[;&]');
}

sub _url_encode_data {
    my $self = shift;
    my ($data, $chr) = @_;

    my @pairs = $data ? split(/$chr/, $data) : ();
    foreach my $pair (@pairs) {
        my ($key, $val) = split(/=/, $pair, 2);
        if (defined $key && defined $val) {
            $key =~ s/\+/ /gs;
            $key =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
                     defined($1) ? chr hex($1) : chr(hex($2))/ge;
            $val =~ s/\+/ /gs;
            $val =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
                     defined($1) ? chr hex($1) : chr(hex($2))/ge;
            $self->{_params}{$key} = $val;
        }
    }
}

# temporary solution
sub header {
    my $self = shift;
    return "HTTP/1.1 200 OK\r\n".
           "Server: Chimera\r\n".
           "Pragma: no-cache\r\n".
           "Expires: Sat, 29 Aug 1987 10:00:00 GMT\r\n".
           "Cache-Control: no-store, no-cache, must-revalidate\r\n".
           "Cache-Control: post-check=0, pre-check=0\r\n".
           "Content-type: text/html\r\n\r\n";
}


1;
